<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2011-10-26
 * @version SVN: $Id$
 */
class Trend_Attachment_ClipVideo
{
    /**
     * 截短视频
     *
     * @param string $filepath 视频文件地址
     * @param string $time_start 截取开始时间，单位：秒
     * @param string $time_end 截取结束时间，单位：秒
     * @return array
     */
    public static function run($filepath, $time_start, $time_end)
    {
        $res = array('status' => 0, 'error' => null, 'data' => null);

        /* 处理需要处理的文件地址 */
        $input = ZEED_PATH_UPLOAD . $filepath;
        $input = str_replace('//', '/', $input);

        /* 处理输出路径 */
        $timenow = Util_Timer::getMicrotime();
        $filepath_arr = explode('.', $filepath);
        $filepath_arr[count($filepath_arr) - 2] .= '_' . $timenow;
        $filepath_arr[count($filepath_arr) - 1] = 'mp4';
        $filepath_new = implode('.', $filepath_arr);
        $output = ZEED_PATH_UPLOAD . $filepath_new;
        $output = str_replace('//', '/', $output);

        /* 处理时间 */
        $time_cha = $time_end - $time_start;
        $time_start = Util_Timer::timeFormat($time_start);
        $time_end = Util_Timer::timeFormat($time_end);

        /* 执行 */
        if ($time_cha > 60) {
            $exec = "ffmpeg -i {$input} -vcodec copy -acodec copy -ss {$time_start} -to {$time_end} {$output} -y && echo success";
        } else {
            $exec = "ffmpeg -i {$input} -acodec copy -vcodec libx264 -preset ultrafast -ss {$time_start} -to {$time_end} {$output} -y && echo success";
        }
        $result = exec($exec);
        if ($result != 'success') {
            $res['status'] = 1;
            $res['error'] = '请检查磁盘读写权限';
        } else {
            $res['data']['filepath'] = $filepath_new;
        }

        return $res;
    }
}

// End ^ Native EOL ^ UTF-8